# Maintainer: Francesco Colista <fcolista@alpinelinux.org>
pkgname=libvirt
pkgver=10.2.0
_ver="${pkgver/_rc/-rc}"
pkgrel=0
pkgdesc="Virtualization API for several hypervisor and container systems"
url="https://libvirt.org/"
arch="all"
options="!check"
license="LGPL-2.1-or-later"
_daemon_deps="bridge-utils dmidecode dnsmasq iptables"
_client_deps="pm-utils gnutls-utils netcat-openbsd"
depends="lvm2"
depends_dev="$pkgname=$pkgver-r$pkgrel"
makedepends="
	$_client_deps
	$_daemon_deps
	$depends_dev
	acl-dev
	augeas-dev
	bash-completion-dev
	curl-dev
	cyrus-sasl-dev
	device-mapper
	e2fsprogs-dev
	eudev-dev
	fuse3-dev
	gnutls-dev
	jansson-dev
	libcap-ng-dev
	libgcrypt-dev
	libgpg-error-dev
	libnl3-dev
	libpcap-dev
	libpciaccess-dev
	libssh2-dev
	libtasn1-dev
	libtirpc-dev
	libxml2-dev
	libxslt-dev
	linux-headers
	lvm2-dev
	lxc-dev
	meson
	netcf-dev
	parted-dev
	perl
	perl-xml-xpath
	polkit-dev
	py3-docutils
	qemu
	qemu-pr-helper
	readline-dev
	rpcsvc-proto
	util-linux
	yajl-dev
	zlib-dev
	"
install="$pkgname.post-install"
subpackages="
	$pkgname-dbg
	$pkgname-libs
	$pkgname-dev
	$pkgname-doc
	$pkgname-client
	$pkgname-daemon
	$pkgname-daemon-openrc
	$pkgname-lang
	$pkgname-lxc
	$pkgname-qemu
	$pkgname-uml::noarch
	$pkgname-vbox
	$pkgname-bash-completion
	"
source="https://libvirt.org/sources/libvirt-$pkgver.tar.xz
	libvirt.confd
	libvirt.initd
	libvirt-guests.confd
	libvirt-guests.initd
	virtlogd.initd
	virtlockd.initd
	"
if [ "$CARCH" = "x86_64" ]; then
	subpackages="$subpackages $pkgname-xen"
	makedepends="$makedepends xen-dev zfs"
fi
subpackages="$subpackages $pkgname-common-drivers:_common_drivers"

# secfixes:
#   7.5.0-r0:
#     - CVE-2021-3631
#   6.8.0-r0:
#     - CVE-2020-25637
#   6.6.0-r0:
#     - CVE-2020-14339
#   5.5.0-r0:
#     - CVE-2019-10168
#     - CVE-2019-10167
#     - CVE-2019-10166
#     - CVE-2019-10161

case "$CARCH" in
arm*|x86)
	;;
*)
	makedepends="$makedepends ceph-dev"
	;;
esac

build() {
	# silence repetitive warnings
	CFLAGS="$CFLAGS -Wno-inline" \
	abuild-meson \
		-Db_lto=true \
		--libexec=/usr/lib/$pkgname \
		-Dinit_script=openrc \
		. output

	meson configure --no-pager output
	meson compile -C output
}

check() {
	meson test --no-rebuild --print-errorlogs -C output
}

package() {
	DESTDIR="$pkgdir" meson install --no-rebuild -C output
	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/libvirtd
	install -Dm644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/libvirtd
	install -Dm755 "$srcdir"/$pkgname-guests.initd "$pkgdir"/etc/init.d/libvirt-guests
	install -Dm644 "$srcdir"/$pkgname-guests.confd "$pkgdir"/etc/conf.d/libvirt-guests

	install -Dm755 "$srcdir"/virtlogd.initd "$pkgdir"/etc/init.d/virtlogd
	install -Dm755 "$srcdir"/virtlockd.initd "$pkgdir"/etc/init.d/virtlockd

	install -d "$pkgdir"/lib/modules-load.d
	echo "tun" > "$pkgdir"/lib/modules-load.d/libvirt.conf

	rm -rf "$pkgdir"/etc/sysconfig
	rm -rf "$pkgdir"/usr/lib/charset.alias
	rm -f "$pkgdir"/usr/lib/sysctl.d/60-qemu-postcopy-migration.conf
	rmdir -p "$pkgdir"/usr/lib 2>/dev/null || true
}

dev() {
	default_dev

	amove usr/share/libvirt/api
}

daemon() {
	cd "$builddir"
	pkgdesc="libvirt daemon package"
	depends="libvirt-client=$pkgver-r$pkgrel $_daemon_deps"
	replaces="libvirt"

	amove lib/modules-load.d
	amove etc/libvirt/libvirtd.conf
	amove usr/sbin/libvirtd
	amove etc/logrotate.d/libvirtd
}

client() {
	cd "$builddir"
	pkgdesc="libvirt client package"
	depends="libvirt=$pkgver-r$pkgrel $_client_deps"
	replaces="libvirt"

	amove etc/libvirt/libvirt.conf
	amove usr/bin
}

_mv_driver() {
	local _name="$1" _drivers="$2" _pkg="$3"
	pkgdesc="$_name driver for libvirt"
	depends="$pkgname-daemon=$pkgver-r$pkgrel
		$pkgname-common-drivers=$pkgver-r$pkgrel"
	replaces="libvirt"
	if [ -n "$_pkg" ]; then
		install_if="$pkgname-daemon=$pkgver-r$pkgrel $_pkg"
	else
		install_if=
	fi
	local _dir=/usr/lib/libvirt/connection-driver
	mkdir -p "$subpkgdir"/$_dir \
		"$subpkgdir"/etc/libvirt \
		"$subpkgdir"/etc/logrotate.d
	local _d
	cd "$pkgdir"
	for _d in $_drivers; do
		# shellcheck disable=1087
		find . -name "*$_d[.-]*" -o -name "*$_d" | while read -r _f; do
			amove $_f
		done
	done
}

qemu() { _mv_driver "QEMU" qemu qemu; }
xen() { _mv_driver "XEN" "libxl xen" xen; }
lxc() { _mv_driver "LXC" lxc lxc; }
uml() { _mv_driver "UML" uml; }
vbox() { _mv_driver "VirtualBox" vbox; }

_common_drivers() {
	pkgdesc="Common libvirt daemon drivers"
	replaces="libvirt"

	amove etc/libvirt/nwfilter
	amove usr/lib/libvirt/connection-driver
}

sha512sums="
65faf2346e66bdba606ba99b9e4da2be8bd94ef42ac49c2801f9e9253ceea10f5fe772d5c7ada59f48aa34ff528726719b690c3acfd3e63a5c3580db17b023ae  libvirt-10.2.0.tar.xz
c3647366e589f2134599baefcbd7ec480cbc0830c71f95cd903cacb28e463d4cd8d972fbd1db81aa17eef81c1f701bd3937c9b8eb71d56ee630e1ed8b89887e9  libvirt.confd
4e28dc733a5db0e102ed529f3215d44c52ce0495dc109e2ef53465a0e205e2ba6f5f0ff56cc6e27318572ca560d778dd24a3faf092e7ab7819674c42876aa764  libvirt.initd
78f419a89de7aabaad860903c24f1c070786fc1296d987e61dd537705b0e7f71a46c315888028f3826c3417e42861ca8471788be4ec5695e98427c5f18ae63fc  libvirt-guests.confd
ec2bc46d72afa61513983b777e2d49da618a35a4687bc75674b3a49ababd4e2e387661010d342fee86c7a73d259644985a263a6c2976e29a436d88921bb040b0  libvirt-guests.initd
f93313c387c21bafb760f599942f1cdecfe952af80af42a28e8525118e5a7fd512d9f909039d8b15f1a544957f1d70a0728c55de2bae41a87e5be6dbbb4f6208  virtlogd.initd
d8ef64d76e8be521a6551893cc7c6aa7e5d8c627a9aed87beefefc126798dc0f98bf420609b6afc027ad96cca347a1c5db8f703d09853efde4d1a6a35572fbf1  virtlockd.initd
"
